//
//  StarView.m
//  EvaluationStar
//
//  Created by 赵贺 on 15/11/26.
//  Copyright © 2015年 赵贺. All rights reserved.
//

#import "StarView.h"

#define labelW 65
#define imageW  (kScreenWidth-38-2*labelW)/10

@interface StarView ()
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) UIView *starBackgroundView;
@property (nonatomic, strong) UIView *starForegroundView;
@property (nonatomic, assign) BOOL canEdit;

@end
@implementation StarView


- (id)initWithFrame:(CGRect)frame leftTitle:(NSString *)title EmptyImage:(NSString *)Empty StarImage:(NSString *)Star canEdit:(BOOL)canEdit starNum:(NSString *)starNum Evaluate:(NSString *)evaluate{

    
    
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, labelW, 30)];
        leftLabel.text = title;
        leftLabel.font = [UIFont systemFontOfSize:14];
        leftLabel.textColor = [UIColor darkTextColor];
        [self addSubview:leftLabel];
        leftLabel.textAlignment = NSTextAlignmentCenter;
        if (_rightLabel == nil) {
            _rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width-labelW, 0, labelW, 30)];
            [self addSubview:_rightLabel];
        }
        _rightLabel.text =@"";
        _rightLabel.hidden = YES;
        _rightLabel.textAlignment = NSTextAlignmentLeft;
        _rightLabel.font = [UIFont systemFontOfSize:14];
        _rightLabel.textColor = [UIColor darkTextColor];
        self.starBackgroundView = [self buidlStarViewWithImageName:Empty];
        self.starForegroundView = [self buidlStarViewWithImageName:Star];
        [self addSubview:self.starBackgroundView];
        
        self.userInteractionEnabled = YES;
        
        /**点击手势*/
        UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [ self addGestureRecognizer:tapGR];

        
        /**滑动手势*/
        
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [ self.starBackgroundView addGestureRecognizer:panGR];
        _canEdit = canEdit;
        if (canEdit == NO) {
        
            int star = [starNum intValue];
            if (star == 1) {
                self.starForegroundView.frame = CGRectMake(38+labelW, 0, star*imageW, 30);
            }else{
                self.starForegroundView.frame = CGRectMake(38+labelW, 0, star*2*imageW, 30);
            }
            
            [self addSubview:self.starForegroundView];
            _rightLabel.hidden = NO;
            if (star <0) {
                star = 0;
            }
            if (star >5) {
                star = 5;
            }
            switch (star) {
                case 1:
                    _rightLabel.text = @"差";
                    break;
                case 2:
                    _rightLabel.text = @"一般";
                    break;
                case 3:
                    _rightLabel.text = @"好";
                    break;
                case 4:
                    _rightLabel.text = @"很好";
                    break;
                case 5:
                    _rightLabel.text = @"非常好";
                    break;
               default:
                    break;
            }
        }
    }
    
    return self;
    
}


- (UIView *)buidlStarViewWithImageName:(NSString *)imageName
{
//    CGRect frame = self.bounds;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(38+labelW, 0, kScreenWidth-38-2*labelW, 30)];
    
    view.clipsToBounds = YES;
    
    for (int j = 0; j < 5; j ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(2*j*imageW, 0, imageW, imageW);
        [view addSubview:imageView];
    }
    
    return view;
}

-(void)tapGR:(UITapGestureRecognizer *)tapGR{
    if (_canEdit == NO) {
        return;
    }
    int X = 0;
    CGPoint point =[tapGR locationInView:self];
    if ((point.x)<38+labelW||point.x>kScreenWidth-labelW) {
        
        return;
    }
    NSLog(@"imageW = %f",imageW);
    if (point.x>38+labelW && point.x < 1.5*imageW+labelW+38) {
         X = 1;
        self.starForegroundView.frame = CGRectMake(38+labelW, 0, imageW, 30);
       
    }else{
        X =  (point.x-38-labelW)/(2*imageW)+1;
        self.starForegroundView.frame = CGRectMake(38+labelW, 0, X*2*imageW, 30);
    }
    
    [self addSubview:self.starForegroundView];
    _rightLabel.hidden = NO;
    _rightLabel.text = @"";
    
    if (X>5) {
        X = 5;
    }
    
    if (X<0) {
        X = 0;
    }
    
    switch (X) {
    
        case 1:
            _rightLabel.text = @"差";
            break;
        case 2:
            _rightLabel.text = @"一般";
            break;
        case 3:
            _rightLabel.text = @"好";
            break;
        case 4:
            _rightLabel.text = @"很好";
            break;
        case 5:
            _rightLabel.text = @"非常好";
            break;
        default:
            break;
    }
    
    NSString *str = [NSString stringWithFormat:@"%d",X];
     __weak StarView *wself = self;
    if(wself.block){
        wself.block(str);
    }

}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
@end
